#include "licence_lib.h"

int check(const char* licenceC, const char* osC, const char* packageNameC, const char* simIDC, const char* actualDateC) {

	// Error return :
	// 0 : OK
	// 1 : bad OS
	// 2 : bad package name
	// 3 : bad sim ID
	// 4 : wrong expiration date
	int result = 0;

	// Licence content decryption
	int length = strlen(licenceC);
	char decryptedLicence[length+1];
	int i;
	for (i=0; i<length; i++) {
		decryptedLicence[i] = decode(licenceC[i], 8);
	}
	decryptedLicence[length] = '\0';


	// Token comparison
	char* token;
	token = strtok(decryptedLicence, "/");
	// token 1 : OS
	if (strcmp(token, osC) != 0) {
		result = 1;
	}
	token = strtok(NULL, "/");
	// token 2 : packageName
	if (strcmp(token, packageNameC) != 0) {
//		__android_log_write(ANDROID_LOG_ERROR, "token package", token);
//		__android_log_write(ANDROID_LOG_ERROR, "packageC", packageNameC);
		result = 2;
	}
	token = strtok(NULL, "/");
	// token 3 : sim ID
	if (strcmp(token, simIDC) != 0) {
		result = 3;
	}
	token = strtok(NULL, "/");
	// token 4 : expiration date
	if (atoi(actualDateC) >= atoi(token)) {
		result = 4;
	}

	return result;

}
